    //
//  CMQGDefaultColorViewController.m
//  Quick Graph
//
//  Created by Alex Restrepo on 5/11/11.
//  Copyright 2011 KZLabs http://kzlabs.me
//  All rights reserved.
//

#import "KZDefaultColorViewController.h"
#import "UIColor-Expanded.h"
#import "CmdData.h"
#import "BLEUtility.h"

@implementation KZDefaultColorViewController
@synthesize delegate;
@synthesize selectedColor;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

// Implement loadView to create a view hierarchy programmatically, without using a nib.
//- (void)loadView 
//{
//	UIView *container = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
//	container.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//	container.backgroundColor = [UIColor clearColor];
//	self.view = container;
//	[container release];
//	
//	KZColorPicker *picker = [[KZColorPicker alloc] initWithFrame:container.bounds];
//    picker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//	picker.selectedColor = self.selectedColor;
//    picker.oldColor = self.selectedColor;
//	[picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
//	[self.view addSubview:picker];
//    [picker release];
//}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat height= self.view.frame.size.height;
    CGFloat width=self.view.frame.size.width;
    NSLog(@"height=%f,width=%f",height,width);
    
    CGRect rect;
    rect = [[UIApplication sharedApplication] statusBarFrame];
    CGFloat statusHeight=rect.size.height;
    
    int naviHeight=self.navigationController.navigationBar.frame.size.height;
    
    int startHeight=naviHeight+statusHeight;
    //    KZColorPicker *picker = [[KZColorPicker alloc] initWithFrame:container.bounds];
    KZColorPicker *picker=[[KZColorPicker alloc] initWithFrame:CGRectMake(0,startHeight, width, height-startHeight)];
    picker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    picker.selectedColor = self.selectedColor;
    picker.oldColor = self.selectedColor;
    [picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:picker];
    
    NSLog(@"alloccccccccc");
    isThreadRunning=false;
    mSendThread=[[NSThread alloc]initWithTarget:self selector:@selector(sendThreadRun) object:nil];
   // mLock=[[NSLock alloc] init];
    isValueChanged=false;
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
//    
    NSLog(@"alloccccccccc!!!!!!!");
//    [mLock release];
//    mLock=nil;
//    [mSendThread release];
//    mSendThread=nil;
}
-(void)viewDidAppear:(BOOL)animated
{
    [mSendThread start];
    isThreadRunning=true;
    isValueChanged=false;
}
-(void)viewDidDisappear:(BOOL)animated
{
    [mSendThread cancel];
    isThreadRunning=false;
    isValueChanged=false;
    
}
-(void)sendThreadRun
{
    isThreadRunning=true;
    while ( isThreadRunning==true) {
        usleep(70*1000);
       // [mLock lock];
        if (isValueChanged) {
            
            int len=6+8;
            uint8_t *pData=malloc(len);
            
            [CmdData getRGBCmdArray:sendColor colorB:sendColor retData:pData];
            
            NSData *data=[[NSData alloc]initWithBytes:pData length:len];
            free(pData);
            
            [BLEUtility writeData:data];
            
            NSUInteger  cout=[data retainCount];
            NSLog(@"sendThreadRun is %d,count is> %d",sendColor,cout);
          
            if(cout>0){
                 [data release];
                data=nil;
            }
            isValueChanged=false;
        }
       // [mLock unlock];
    }
}



//- (void)dealloc
//{
//    [selectedColor release];
//    [super dealloc];
//}

- (void) pickerChanged:(KZColorPicker *)cp
{	
    self.selectedColor = cp.selectedColor;
    [delegate defaultColorController:self didChangeColor:cp.selectedColor];

    
    CGFloat R,G,B,A;
    
    [self.selectedColor getRed:&R green:&G blue:&B alpha:&A];
    
     uint8_t charR,charG,charB,charA;
    
    charR=(uint8_t)(R*255);
    charG=(uint8_t)(G*255);
    charB=(uint8_t)(B*255);
    charA=(uint8_t)(A*255);

   // [mLock lock];
    int color=charR<<16 | charG<<8 | charB;
    sendColor=color;
    isValueChanged=true;
    //[mLock unlock];
}

#pragma mark -
#pragma mark  Popover support
- (CGSize) contentSizeForViewInPopover
{       	
	return CGSizeMake(320, 416);
}

@end
